package com.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blog.common.Result;
import com.blog.mapper.BlogMapper;
import com.blog.mapper.UserMapper;
import com.blog.pojo.Blog;
import com.blog.pojo.Type;
import com.blog.pojo.User;
import com.blog.service.BlogService;
import com.blog.service.TypeService;
import com.blog.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据统计相关
 */
@Slf4j
@RestController
@RequestMapping("/Echarts")
public class EchartsController {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private BlogService blogService;

    @Autowired
    private BlogMapper blogMapper;

    @Autowired
    private TypeService typeService;

    /**
     * 按每年的月份统计注册用户数量
     *
     * @return Result<Map < String, Integer>>
     */
    @GetMapping("/newUserCountGroupByMonth")
    private Result<Map<String, Integer>> newUserCountGroupByMonth() {
        List<HashMap<String, Object>> hashMaps = userMapper.newUserCountGroupByMonth();
        if (hashMaps != null && !hashMaps.isEmpty()) {//有数据
            //遍历hashMapList
            /*Map<String,Integer> map = new HashMap<>();
            for (HashMap<String, Object> hashMap : hashMaps) {
                String key = null;
                Integer value = null;
                for (Map.Entry<String,Object> entry : hashMap.entrySet()){
                    if ("key".equals(entry.getKey())){
                        key = (String) entry.getValue();
                    } else if ("value".equals(entry.getKey())){
                        //我需要的是int型所以做了如下转换，实际上返回的object应为Long
                        value = ((Long)entry.getValue()).intValue();
                    }
                }
                map.put(key,value);
            }
            return Result.success(map);*/
            return Result.success(this.mapListToMap(hashMaps));
        }
        //没有数据
        return Result.error("不好意思，没有查询到相关信息...");
    }

    /**
     * 统计每个类型下的博客数量
     * 键：类型名称
     * 值：博客数量
     * @return Result<Map < String, Integer>>
     */
    @GetMapping("/blogGroupByType")
    private Result<Map<String, Integer>> blogGroupByType() {
        List<HashMap<String, Object>> hashMaps = blogMapper.blogGroupByType();
        if (hashMaps != null && !hashMaps.isEmpty()) {//有数据
            //遍历hashMapList
            Map<String,Integer> map = new HashMap<>();
            for (HashMap<String, Object> hashMap : hashMaps) {
                String key = null;
                Integer value = null;
                for (Map.Entry<String,Object> entry : hashMap.entrySet()){
                    if ("key".equals(entry.getKey())){
                        //获取对应type的name，同时设置给key
                        Type typeServiceById = typeService.getById((Long) entry.getValue());
                        key = typeServiceById.getTypeContent();
                    } else if ("value".equals(entry.getKey())){
                        //我需要的是int型所以做了如下转换，实际上返回的object应为Long
                        value = ((Long)entry.getValue()).intValue();
                    }
                }
                map.put(key,value);
            }
            return Result.success(map);
        }
        //没有数据
        return Result.error("不好意思，没有查询到相关信息...");
    }

    /**
     * 所有已发布，未发布，草稿状态的博客数量
     * @return Result<Map<String, Integer>>
     */
    @GetMapping("/blogGroupByStatus")
    private Result<Map<String, Integer>> blogGroupByStatus() {
        List<HashMap<String, Object>> hashMaps = blogMapper.blogGroupByStatus();
        if (hashMaps != null && !hashMaps.isEmpty()) {//有数据
            Map<String, Integer> map = new HashMap<>();
            for (HashMap<String, Object> hashMap : hashMaps) {
                String key = null;
                Integer value = null;
                for (Map.Entry<String, Object> entry : hashMap.entrySet()) {
                    if ("key".equals(entry.getKey())) {
                        //key = (String) entry.getValue();
                        //这里不能像上述那样写，因为put的时候是integer 那么可以直接强转不会报错
                        //但是如果你放进去的时候是字符串，内容是数字，你强转String就要报标题的错误了
                        //可以有toString解决
                        key = entry.getValue().toString();
                    } else if ("value".equals(entry.getKey())) {
                        //我需要的是int型所以做了如下转换，实际上返回的object应为Long
                        value = ((Long) entry.getValue()).intValue();
                    }
                }
                map.put(key, value);
            }
            return Result.success(map);
        }
        //没有数据
        return Result.error("不好意思，没有查询到相关信息...");
    }

    /**
     * 分组查询通用方法：结果hashMapList转换为Map
     *
     * @param hashMaps 分组查询结果hashMapList
     * @return Map<String, Integer>
     */
    private Map<String, Integer> mapListToMap(List<HashMap<String, Object>> hashMaps) {
        Map<String, Integer> map = new HashMap<>();
        for (HashMap<String, Object> hashMap : hashMaps) {
            String key = null;
            Integer value = null;
            for (Map.Entry<String, Object> entry : hashMap.entrySet()) {
                if ("key".equals(entry.getKey())) {
                    key = (String) entry.getValue();
                } else if ("value".equals(entry.getKey())) {
                    //我需要的是int型所以做了如下转换，实际上返回的object应为Long
                    value = ((Long) entry.getValue()).intValue();
                }
            }
            map.put(key, value);
        }
        return map;
    }

    /**
     * 返回用户总数
     * @return Result<Integer>
     */
    @GetMapping("/userTotal")
    private Result<Integer> userTotal(){
        return Result.success(userMapper.selectCount(null));
    }

    /**
     * 返回非草稿的博客总数
     * @return Result<Integer>
     */
    @GetMapping("/blogTotal")
    private Result<Integer> blogTotal(){
        QueryWrapper<Blog> blogQueryWrapper1 = new QueryWrapper<>();
        //里面
        blogQueryWrapper1.eq("blogStatus",1).or().eq("blogStatus",2);
        return Result.success(blogMapper.selectCount(blogQueryWrapper1));
    }

    /**
     * 返回所有博客访问量之和
     * @return Result<Integer>
     */
    @GetMapping("/blogVisitTotal")
    private Result<Integer> blogVisitTotal(){
        QueryWrapper<Blog> blogQueryWrapper = new QueryWrapper<>();
        blogQueryWrapper.select("sum(visitCount) as visitTotal");
        return Result.success(blogService.getOne(blogQueryWrapper).getVisitTotal());
    }
}
